//
//  LKChoosePKPeopleViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/18.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKChoosePKPeopleViewController.h"
#import "LKPKPeopleCell.h"
#import "LKPKDataCompareViewController.h"
@interface LKChoosePKPeopleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,assign)NSInteger selectNumber;
@end

@implementation LKChoosePKPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectNumber = 0;
    [self setCollectionView];
    
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"选择对象";
    [self.backButton setTitleColor:[UIColor whiteColor]];
    [self.backButton setImage:[UIImage imageNamed:@"back2"] forState:UIControlStateNormal];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:blueDefault];
    //设置导航条文字颜色 白色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.delegate = self;
}
-(void)setCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(127, 32);
    self.flowLayout.minimumLineSpacing = 24;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LKPKPeopleCell" bundle:nil] forCellWithReuseIdentifier:@"LKPKPeopleCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [self.collectionView reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LKPKPeopleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LKPKPeopleCell" forIndexPath:indexPath];
    cell.chooseBtn.layer.cornerRadius = 16;
    cell.chooseBtn.layer.masksToBounds = YES;
    cell.chooseBtn.layer.borderWidth = 1;

    [cell.chooseBtn addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = blueDefault;
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.text = @"最少选择两个,最大选择3个同时进行对比,请选择对象";
        [header addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(header.mas_left).offset(9);
            make.top.equalTo(header.mas_top).offset(9);
        }];
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = COLOR_HEX(0xE4E4E4);
        [header addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(9);
            make.size.mas_equalTo(CGSizeMake(WIDTH, 1));
            make.left.equalTo(header);
        }];
        return header;
    }else{
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [startBtn setTitle:@"开始对比" forState:UIControlStateNormal];
        startBtn.backgroundColor = blueDefault;
        [startBtn addTarget:self action:@selector(gotoPK:) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:startBtn];
        [startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(footer);
            make.centerX.equalTo(footer);
            make.left.equalTo(footer).offset(33);
            make.height.mas_equalTo(45);
        }];
        startBtn.layer.cornerRadius = 23;
        startBtn.layer.masksToBounds = YES;
        return footer;
    }
}
-(void)gotoPK:(UIButton *)btn{
    if (self.selectNumber < 2) {
        [SVProgressHUD showInfoWithStatus:@"至少两个"];
        return;
    }
    if (self.selectNumber > 3) {
        [SVProgressHUD showInfoWithStatus:@"最多三个"];
        return;
    }
    LKPKDataCompareViewController *vc = [[LKPKDataCompareViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, 70);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, 110);
}
-(void)chooseAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        btn.layer.borderColor = blueDefault.CGColor;
        self.selectNumber++;
    }else{
        btn.layer.borderColor = COLOR_HEX(0xE0E0E0).CGColor;
        self.selectNumber--;
    }

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 55, 0, 55);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
